package com.yujiahao.bigdata.wordcount

import org.apache.spark.rdd.RDD
import org.apache.spark.{SparkConf, SparkContext}

//
object Spark01_WordCount3 {
  def main(args: Array[String]): Unit = {
    //TODO 1、获得连接
    val conf: SparkConf = new SparkConf().setAppName("WordCount").setMaster("local")
    val sc: SparkContext = new SparkContext(conf)
    val lineRDD: RDD[String] = sc.textFile("data/word.txt")
    val wordRDD: RDD[String] = lineRDD.flatMap(_.split(" "))
    val wordToOneRDD: RDD[(String, Int)] = wordRDD.map((_, 1))
    val wordCountRDD: RDD[(String, Int)] = wordToOneRDD.reduceByKey(_ + _)
    wordCountRDD.collect().foreach(println)

    //TODO 2、关闭连接
    sc.stop()

  }

}
